package org.e_gold.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static String getDateByPattern(String pattern) {

		return getDateByPattern(pattern, new Date());
	}

	public static String getDateByPattern(String pattern, Date date) {

		return new SimpleDateFormat(pattern).format(date);
	}

	public static Date getDateByPattern(String pattern, String date){
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}

	public static String getDate() {

		return getDateByPattern("dd-MM-YYYY HH:mm:ss");
	}

	public static String getDate(Date d){
		String pattern = "YYYY-MM-dd HH:mm:ss";
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);

		return new SimpleDateFormat(pattern).format(d);
	}

	/**
	 * Allow user to get date from history range to future range
	 * @param range There are 2 way for provide: 
	 * <ul><li>an integer number for fixed range</li><li>a double as 0.5 for half day, 0.25 for a quarter of day</li></ul>
	 * @return an array[2] with <br/>array[0] = dateFrom<br/>array[1] = dateTo
	 * @author Thang Nguyen
	 */
	public static Date[] getDateInRange(Number range){
		//accept 1/2 day; 1/3 day, ^.^
		Double dRange = range.doubleValue();
		dRange *= 24;

		return getHourInRange(dRange.intValue());
	}

	public static Date[] getDateInRange(Number range, Date currentDate){
		//accept 1/2 day; 1/3 day, ^.^
		Double dRange = range.doubleValue();
		dRange *= 24;

		return getHourInRange(dRange.intValue(), currentDate);
	}

	public static Date[] getHourInRange(Number range){
		Double dRange = range.doubleValue();
		dRange *= 60;

		return getMinuteInRange(dRange.intValue());
	}

	public static Date[] getHourInRange(Number range, Date currentDate){
		Double dRange = range.doubleValue();
		dRange *= 60;

		return getMinuteInRange(dRange.intValue(), currentDate);
	}

	public static Date[] getMinuteInRange(Number range){
		Double dRange = range.doubleValue();
		dRange *= 60;

		return getSecondInRange(dRange.intValue());
	}

	public static Date[] getMinuteInRange(Number range, Date currentDate){
		Double dRange = range.doubleValue();
		dRange *= 60;

		return getSecondInRange(dRange.intValue(), currentDate);
	}

	public static Date[] getSecondInRange(int range){
		Date date = new Date();

		return getSecondInRange(range, date);
	}

	public static Date[] getSecondInRange(int range, Date currentDate){
		Date[] ranges = new Date[2];

		Calendar cal = Calendar.getInstance();
		cal.setTime(currentDate);

		//get future hour
		cal.add(Calendar.SECOND, range);
		Date dTo = cal.getTime();

		//get previous hour
		cal.add(Calendar.SECOND, (range*2*-1));
		Date dFrom = cal.getTime();

		ranges[0] = dFrom;
		ranges[1] = dTo;

		return ranges;
	}



	public static void main(String[] args) throws ParseException {
		System.out.println("-----getDateInRange--------");
		Date[] d4 = getDateInRange(1);
		Date[] d5 = getDateInRange(2);
		Date[] d6 = getDateInRange(3);

		System.out.println(d4[0] + "\t" + d4[1]);
		System.out.println(d5[0] + "\t" + d5[1]);
		System.out.println(d6[0] + "\t" + d6[1]);

		System.out.println("------getHourInRange-------");

		Date[] h1 = getHourInRange(1);
		Date[] h2 = getHourInRange(3);
		Date[] h3 = getHourInRange(12);

		System.out.println(h1[0] + "\t" + h1[1]);
		System.out.println(h2[0] + "\t" + h2[1]);
		System.out.println(h3[0] + "\t" + h3[1]);

		System.out.println("------getMinuteInRange-------");

		Date[] m1 = getMinuteInRange(1);
		Date[] m2 = getMinuteInRange(3);
		Date[] m3 = getMinuteInRange(12);

		System.out.println(m1[0] + "\t" + m1[1]);
		System.out.println(m2[0] + "\t" + m2[1]);
		System.out.println(m3[0] + "\t" + m3[1]);

		System.out.println("------getSecondInRange-------");

		Date[] s1 = getSecondInRange(1);
		Date[] s2 = getSecondInRange(3);
		Date[] s3 = getSecondInRange(12);

		System.out.println(s1[0] + "\t" + s1[1]);
		System.out.println(s2[0] + "\t" + s2[1]);
		System.out.println(s3[0] + "\t" + s3[1]);

		System.out.println(getDateByPattern("yyyy-MM-dd HH:mm:ss", "2014-06-15 16:16:07"));
	}
}
